package com.iemes.controller.mould;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.MouldUnpassHandleFormMap;
import com.iemes.entity.PAfterCheckFormMap;
import com.iemes.entity.PUnloadcheckFromMap;
import com.iemes.entity.PinstallcheckFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.mould.MouldUnpassHandleMapper;
import com.iemes.mapper.mould.PUnloadcheckMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/pUnloadcheck/")
@Controller
public class PUnloadcheckController  extends BaseController {

	@Inject
	private PUnloadcheckMapper pUnloadcheckMapper;
	
	@Inject
	private MouldUnpassHandleMapper mouldUnpassHandleMapper;
	
	@RequestMapping("examinePunloadcheck")
	public String examineOrder(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/pUnloadcheck/pUnloadcheck_list";
	}
	
	@ResponseBody
	@RequestMapping("pUnload_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		PUnloadcheckFromMap punloadFromMap = getFormMap(PUnloadcheckFromMap.class);
		List<Map<String, Object>> entryFormMaps=pUnloadcheckMapper.getAll(punloadFromMap);
		LuogicPaging(pageNow, pageSize, entryFormMaps);
		return pageView;
	}
	
	@RequestMapping("pUnloadCheck_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			System.out.println("id："+id);
			//根据id查询备模后模具质量表
			Map<String, Object> unload = pUnloadcheckMapper.getById(id);
			model.addAttribute("unload",unload);
			//获取全部用户
			UserFormMap userFormMap=getFormMap(UserFormMap.class);
			List<UserFormMap> users=baseMapper.findByNames(userFormMap);
			model.addAttribute("users",users);
			//获得相同checkId的检查标准
			MouldUnpassHandleFormMap mouldUnpassHandleFormMap=getFormMap(MouldUnpassHandleFormMap.class);
			mouldUnpassHandleFormMap.put("checkId", unload.get("id"));
			List<MouldUnpassHandleFormMap> handle=mouldUnpassHandleMapper.getUnloadHandle(mouldUnpassHandleFormMap);
			model.addAttribute("handle",handle);
		}
		return Common.BACKGROUND_PATH + "/mould/pUnloadcheck/pUnloadcheck_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="pUnloadCheck_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="备模后模具质量检查",methods="备模后模具质量检查表-修改备模后模具质量检查表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		PUnloadcheckFromMap pUnloadcheckFromMap=getFormMap(PUnloadcheckFromMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		pUnloadcheckFromMap.put("checkDate", sdf.format(date));
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		pUnloadcheckFromMap.put("checkPeopleId", userFormMap.getInt("id"));
		baseMapper.editEntity(pUnloadcheckFromMap);
		//先删除
		baseMapper.deleteByAttribute("checkId","'"+Integer.parseInt(pUnloadcheckFromMap.getStr("id"))+"'", MouldUnpassHandleFormMap.class);
		//再添加
		int checkId=Integer.parseInt(request.getParameter("pUnloadcheckFromMap.id"));
		String[] standardId=request.getParameterValues("standardId_1");
		String[] checkResult=request.getParameterValues("checkResult_1");
		String[] handle=request.getParameterValues("handle_1");
		String[] handleTime=request.getParameterValues("handleTime_1");
		String[] repairPeopleId=request.getParameterValues("repairPeopleId_1");
		String[] repairResult=request.getParameterValues("repairResult_1");
		String[] remark=request.getParameterValues("remark_1");
		String[] finishRepair=request.getParameterValues("finishRepair_1");
		String[] finishTime=request.getParameterValues("finishTime_1");
		if(standardId!=null){
			for(int i=0;i<standardId.length;i++){
				MouldUnpassHandleFormMap mouldUnpassHandleFormMap=new MouldUnpassHandleFormMap();
				mouldUnpassHandleFormMap.put("checkId", checkId);
				mouldUnpassHandleFormMap.put("standardId", standardId[i]);
				mouldUnpassHandleFormMap.put("checkResult", checkResult[i]);
				mouldUnpassHandleFormMap.put("handle",handle[i]);
				mouldUnpassHandleFormMap.put("handleTime", handleTime[i]);
				mouldUnpassHandleFormMap.put("repairPeopleId", Integer.parseInt(repairPeopleId[i]));
				mouldUnpassHandleFormMap.put("repairResult",repairResult[i]);
				mouldUnpassHandleFormMap.put("remark",remark[i]);
				mouldUnpassHandleFormMap.put("finishRepair", Integer.parseInt(finishRepair[i]));
				mouldUnpassHandleFormMap.put("finishTime",finishTime[i]);
				mouldUnpassHandleFormMap.put("supervisorId", userFormMap.getInt("id"));
				baseMapper.addEntity(mouldUnpassHandleFormMap);
			}
		}
		return "success";
	}
}
